package com.mingzhi.permissionservice.repository;

import com.mingzhi.permissionservice.entity.UserRoles;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PermissionRepository {

    //根据用户id从数据库获取角色码
    @Select("SELECT r.role_code FROM roles r JOIN user_roles ur ON r.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId}")
    String getRoleCode(Long userId);


    @Insert("insert into user_roles(user_id,role_id) values(#{userId},2)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer bindDefaultRole(UserRoles userRoles);

    //根据用户id查询用户角色id
    @Select("SELECT role_id FROM user_roles WHERE user_id = #{userId}")
    Integer getRoleIdByUserId(Long userId);

    @Select("SELECT user_id FROM user_roles WHERE role_id = #{roleId}")
    List<Long> getUserIdsByRoleId(int roleId);

    //升级
    @Update("UPDATE user_roles SET role_id = 3 WHERE user_id = #{userId}")
    void upgradeToAdmin(Long userId);

    //降级
    @Update("UPDATE user_roles SET role_id = 2 WHERE user_id = #{userId}")
    void downgradeToUser(Long userId);
}
